package com.micromall.service;

import com.micromall.repository.CashAccountMapper;
import com.micromall.repository.CashRecordMapper;
import com.micromall.repository.entity.CashAccount;
import com.micromall.repository.entity.CashRecord;
import com.micromall.repository.entity.common.CashChangeTypes;
import com.micromall.repository.entity.common.CashTypes;
import com.micromall.utils.LogicException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author zhangzxiang91@gmail.com
 * @date 2016/04/17.
 */
@Service
@Transactional
public class CashAccountService {

	@Resource
	private CashAccountMapper mapper;
	@Resource
	private CashRecordMapper  cashRecordMapper;

	@Transactional(readOnly = true)
	public CashAccount getCashAccount(Integer uid) {
		return mapper.selectByPrimaryKey(uid);
	}

	public void createCashAccount(Integer uid) {
		CashAccount cashAccount = new CashAccount();
		cashAccount.setUid(uid);
		cashAccount.setBalance(BigDecimal.ZERO);
		cashAccount.setAvailableCommission(BigDecimal.ZERO);
		cashAccount.setTotalCommission(BigDecimal.ZERO);
		cashAccount.setTotalSales(BigDecimal.ZERO);
		cashAccount.setCreateTime(new Date());
		mapper.insert(cashAccount);
	}

	public void changeBalance(int uid, int amount, String reason) {
		CashAccount cashAccount = getCashAccount(uid);
		if (cashAccount == null) {
			throw new LogicException("会员账户信息错误");
		}
		if (amount > 0) {
			mapper.incrementBalance(uid, new BigDecimal(amount), new Date());
		} else {
			if (mapper.decrementBalance(uid, new BigDecimal(Math.abs(amount)), new Date()) == 0) {
				throw new LogicException("账户余额不足");
			}
		}
		// 更新账余额
		cashAccount = getCashAccount(uid);

		// 账户资金变动记录
		CashRecord cashRecord = new CashRecord();
		cashRecord.setUid(uid);
		cashRecord.setCashType(CashTypes.会员账户余额);
		cashRecord.setAmount(new BigDecimal(amount));
		cashRecord.setBalance(cashAccount.getBalance());
		cashRecord.setCommission(cashAccount.getAvailableCommission());
		cashRecord.setType(CashChangeTypes.后台调整);
		cashRecord.setContent(reason);
		cashRecord.setCreateTime(new Date());
		cashRecordMapper.insert(cashRecord);
	}

	public int decrementBalance(int uid, BigDecimal amount) {
		return mapper.decrementBalance(uid, amount, new Date());
	}

	public int incrementBalance(int uid, BigDecimal amount) {
		return mapper.incrementBalance(uid, amount, new Date());
	}

	public int decrementCommission(int uid, BigDecimal amount) {
		return mapper.decrementCommission(uid, amount, new Date());
	}

	public int incrementCommission(int uid, BigDecimal amount) {
		return mapper.incrementCommission(uid, amount, new Date());
	}

	public int retreatCommission(int uid, BigDecimal amount) {
		return mapper.retreatCommission(uid, amount, new Date());
	}

	public int incrementTotalSales(int uid, BigDecimal amount) {
		return mapper.incrementTotalSales(uid, amount, new Date());
	}
}
